import $service from '../../../../service.js';
var hospitalization_id;
var user_type = "1";
var user_id;
var type;
var that;
var imgurl;
Page({
    data:{
       active : true,
       hospitalizationMap:{},
       inquiryMap:{},
       doctorMap:{},
       type:0,
       hospitalization_imgurl:''
       
    },
    onLoad(e){
        hospitalization_id = e.hospitalization_id;
        if($service.isUndefinedAndEmpty(e.type)){
            e.type = 0
        }
        this.setData({
            type:e.type
        })
    },
    onShow() {
        that =this.data
        if(!$service.isUndefinedAndEmpty($service.getUserLocalStorage().user_type)){
        		user_type = $service.getUserLocalStorage().user_type;
        	}
         if(user_type == 1){//1用户 1门店
             user_id = $service.getUserLocalStorage().user_id;
         }else{
             user_id = $service.getUserLocalStorage().store_id;
         }
        this.publicUtil_getInquiryLogHospitalizationDetails()
    },
    publicUtil_getInquiryLogHospitalizationDetails:function(){
        var this_ = this;
        var jsonData ={
            "hospitalization_id" : hospitalization_id
        }
        var jsonAjax = {
            "url": "publicUtil_getInquiryLogHospitalizationDetails.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.getInquiryLogHospitalizationDetails_back);
    },
    getInquiryLogHospitalizationDetails_back:function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data;
        var doctorMap = data.doctorMap;//医生信息
        var hospitalizationMap = data.hospitalizationMap;//住院
        hospitalizationMap.imgurl ='/' + hospitalizationMap.imgurl
        imgurl = $service.getUserImgPub(hospitalizationMap.imgurl)
        var inquiryMap = data.inquiryMap;//诊断信息
        inquiryMap.lrsj = $service.nothinglrsj(inquiryMap.lrsj)
        this.setData({
            hospitalizationMap:hospitalizationMap,
            doctorMap:doctorMap,
            inquiryMap:inquiryMap,
            hospitalization_imgurl:imgurl
        })
    },
    previewImage: function(e) { //图片放大
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        
        var all = [];
        all.push(this.data.hospitalization_imgurl);
        all.forEach(item => {
          item = item.replace(/https/, 'http')
        })
        wx.previewImage({
            urls: all
        })
    },
    hospitalizationTap(e){
        console.log(e)
       var num = e.currentTarget.dataset.num;
        if(num == 1){
                var imgSrc = imgurl;
                if(!$service.isUndefinedAndEmpty(imgurl)){
                    wx.downloadFile({
                      url: imgSrc,
                      success: function (res) {
                        console.log(res);
                        //图片保存到本地
                        wx.saveImageToPhotosAlbum({
                          filePath: res.tempFilePath,
                          success: function (data) {
                            wx.showToast({
                              title: '保存成功',
                              icon: 'success',
                              duration: 2000
                            })
                          },
                          fail: function (err) {
                            console.log(err);
                            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                              console.log("当初用户拒绝，再次发起授权")
                              wx.openSetting({
                                success(settingdata) {
                                  console.log(settingdata)
                                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                    console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                                  } else {
                                    console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                                  }
                                }
                              })
                            }
                          },
                          complete(res){
                            console.log(res);
                          }
                        })
                      }
                    })
                }else{
                    $service.showToast('未生成成功')
                }
                    
        }else{
            wx.navigateBack({
                delta:1
            })
        }
        
    },
   
    
})